package backupfire;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.SQLException;
import javax.swing.*;
import  bd.Database;
import  bd.Settings;
import  bd.EditSettings;
import common.TreeView;
import  java.io.File;

public class Main extends  JFrame implements ActionListener
{
    /**
     * 
     */
    private String fileSettings= "settings.dat";
    
    private static final long serialVersionUID = 69251788769613327L;
    
    public Main() 
    {
        Database bd = new Database();
        Settings settings = new Settings();
        File file = new File(fileSettings);
        
        if(!file.exists())
        {
            EditSettings edt = new EditSettings(fileSettings);
            edt.setVisible(true);
        }   
        
        Login log = new Login();
        log.setVisible(true);
        initComponents();
    }
    
    public static void main(String[] args) throws SQLException
    {
        new Main().setVisible(true);
    }   

    private void initComponents()
    {
        int width=800, heigth=600;
        int temp;
        
        setSize(width, heigth);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        /* Make the Menu*/
        createMainMenu();
        
        loadProject();
        
        /*Separate the components in JSplitPane*/
        left =  new JScrollPane(tree);
        right = new JScrollPane(desktop);
        split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
        temp = (int)(width - (width * 0.70));        /*    30% of Frame  */        
        split.setDividerLocation(temp);
        
        add(split);        
    }
    
    
    private void createMainMenu()
    {
        /*Menu File*/
        String []itemsFile = {"File","New Project","Open Project","-","New File", "Open File", "Close File", "-","Quit"};
        JMenu menuFile = makeMenu(itemsFile);
        
        /*Menu Generate*/
        String []itemsGenerate = {"Generate","Object" ,"Form","Application"};
        JMenu menuGenerate = makeMenu(itemsGenerate);
        
        /*Menu Database*/ 
        String []itemsDatabase = {"Database","New Connection","Open Connection","Close Connection","Engenering Reverse"};
        JMenu menuDatabase = makeMenu(itemsDatabase);
        
        /*Menu Main*/
        JMenuBar menuMain = new JMenuBar();
        menuMain.add(menuFile);
        menuMain.add(menuGenerate);
        menuMain.add(menuDatabase);        
        setJMenuBar(menuMain);        
    } 
    
    public JMenu makeMenu( String items[]) 
    {
        int tam, i;
        
        JMenu menu = new JMenu(items[0]);
        tam = items.length;
        
        for( i=1; i < tam; i++)
        {
            if( items[i].equals("-"))
            {
                menu.addSeparator();
            }
            else
            {
                JMenuItem m = new JMenuItem( items[i] );
                m.addActionListener(this);
                menu.add(m);
            }
        }
        return menu;
    }    
    
    public void actionPerformed(ActionEvent e)
    {
        String cmd = e.getActionCommand();
        
        /*File*/
        if( cmd.equals("Open Project"))
        {
            loadProject();
        }
        else if( cmd.equals("Quit"))
        {
            System.exit(0);
        }
    }
       
    private void loadProject()
    {       
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(chooser.DIRECTORIES_ONLY);
        int result = chooser.showOpenDialog(this);
        if( result == chooser.CANCEL_OPTION) 
            return;
        
        tree = null;
        tree = TreeView.buildTree(chooser);
    }
    
    /*Left Side*/
    JScrollPane tree= new JScrollPane();
    /*Right Side*/
    JDesktopPane desktop = new JDesktopPane();    
    JComponent left;
    JComponent right;
    JSplitPane split;    
}